package com.hengda.museumonline.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.amap.api.services.cloud.CloudItem;
import com.bumptech.glide.Glide;
import com.hengda.museumonline.R;
import com.hengda.museumonline.entity.LeaningBean;
import com.hengda.museumonline.ui.ArchContentActivity;
import com.hengda.museumonline.ui.NewsContentActivity;
import com.hengda.museumonline.utils.GlideCircleTransform;

import java.util.ArrayList;

import static com.yolanda.nohttp.NoHttp.getContext;

/**
 * @author：JFWU on 2016/9/26 18:36
 * @E-mail：jeffy12138@126.com
 */
public class FindAdapter extends RecyclerView.Adapter<FindAdapter.ListHolder> {
    private Context context;
    private LeaningBean leaningBean;

    public FindAdapter(Context context, LeaningBean leaningBean) {
        this.context = context;
        this.leaningBean = leaningBean;
    }

    @Override
    public FindAdapter.ListHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.find_item, parent, false);
        return new ListHolder(view);
    }

    @Override
    public void onBindViewHolder(FindAdapter.ListHolder holder, int position) {
        holder.setData(position);
    }

    @Override
    public int getItemCount() {
        return leaningBean.getData().size();
    }

    class ListHolder extends RecyclerView.ViewHolder {
        private ImageView ivImage;
        private TextView tvTitle;
        private TextView tvTime;
        private TextView tvLocation;
        private CardView find_item;

        public ListHolder(View itemView) {
            super(itemView);
            ivImage = (ImageView) itemView.findViewById(R.id.iv_image);
            tvTitle = (TextView) itemView.findViewById(R.id.tv_title);
            tvTime = (TextView) itemView.findViewById(R.id.tv_time);
            tvLocation = (TextView) itemView.findViewById(R.id.tv_location);
            find_item = (CardView) itemView.findViewById(R.id.find_item);
        }

        public void setData(final int position) {
            tvTitle.setText(leaningBean.getData().get(position).getTitle());
            tvTime.setText(leaningBean.getData().get(position).getDate());
            tvLocation.setText(leaningBean.getData().get(position).getSource());
            Glide.with(context).load(leaningBean.getData().get(position).getPic_url()).transform(new GlideCircleTransform(context)).into(ivImage);
            find_item.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Bundle bundle = new Bundle();
                    bundle.putString("id", leaningBean.getData().get(position).getObject_id());
                    Intent intent = new Intent(getContext(), ArchContentActivity.class);
                    intent.putExtras(bundle);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    getContext().startActivity(intent);
                }
            });
        }


    }
}
